משתנים שעור מס. 2 דרור טובי דר' כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1
תפקיד המשתנים הצהרה על משתנה השמת ערך במשתנה int a, b, c; a = 1234; b = 99; c = a + b; משתנים מאפשרים לנו לשמור מידע בתוכנית 2
סוגי משתנים הצהרת משתנים סוג משתנה int double boolean char String הצהרת משתנים ערכים מספרים שלמים מספרים ממשיים ערכים בוליאנים תווים סדרת תווים דוגמאות 23 56644 4 2.56 3.1244 112.7, w 2 \n A ABC 23 type variable_name_1, variable_name_2,..., variable_name_n; int a; a= 3; char c; c = 3 ; String s; a= 3; 3
מעקב אחרי ערכי משתנים בתוכנית a b c int a, b; לא מוגדר לא מוגדר a = 1234; לא מוגדר 1234 b = 99; 1234 99 int c = a; 1234 99 1234 a = b; 99 99 1234 b = c; 99 1234 1234 4
int פעולת החיבור עבור Strings ו public class IntAndStrings { public static void main(string[] args) { int a = 1234; int b = 56; System.out.println(a + b); // summation of integers String sa = "1234"; String sb = "56"; System.out.println(sa + sb); // summation of Strings // automatic conversion of int to String String sc = "" + a + b; System.out.println(sc); $ java IntAndStrings 1290 123456 123456 פעולת החיבור מוגדרת הסכום עבור מספרים ושרשור עבור מחרוזות 5
מעבר ממחרוזות למספרים public class Sum { השיטות הפיכת מחרוזת למספר Integet.parseInt() Double.parseDouble() תוכנית המקבלת שני מספרים ומדפיסה את סכומם public static void main(string[] args) { int a = Integer.parseInt( args[0] ); int b = Integer.parseInt( args[1] ); System.out.println("The sum is: " + (a + b) ); $ java Sum 5 4 The sum is: 9 6
פעולות חשבון ב integers הערות בחילוק לא קיימת שארית שארית החילוק שגיאה קדימות ל * קדימות ל / חישוב מבוצע משמאל לימין סגנון כתיבה טוב קדימות ל )( תוצאה 8 2 15 1 2 13 5-4 -4 0 ביטוי 5 + 3 5-3 5 * 3 5 / 3 5 % 3 1 / 0 3 * 5-2 3 + 5 / 2 3 5-2 (3 5) - 4 3 (5 2) טווח המספרים מטיפוס int הוא -2 31 : 2 31-1 7
אופרטור המודולו % במספרים שלמים a % b a ( a / b) * b 17 % 3 = 17 (17/3) * 3 17-5 * 3 8
פעולות חשבון ב double תוצאה 3.171 3.111 3.01e23 1.666666 0.577 Infinity 1.4142 NaN ביטוי 3.141 +.03 3.141 -.03 6.02e23 / 2 5.0 / 3.0 10.0 % 3.141 1.0 / 0.0 Math.sqrt(2.0) Math.sqrt(-1.0) טווח המספרים מסוג double הוא: 4.94065645841246544e-324d to 1.79769313486231570e+308d 9
אופרטור המודולו % במספרים ממשיים a % b a (int)( a/ b) * b 10.0 % 3.141 = 10.0 (int)(10.0/ 3.141) * 3.141 10.0 - (int) 3.184 * 3.141 10.0-3 * 3.141 10
חישוב שורשי הפולינום x 2 +bx+c x 2 +bx+c public class Quadratic { תוכנית לחישוב שורשי הפולינום public static void main(string[] args) { double b = Double.parseDouble( args[0] ); double c = Double.parseDouble( args[1] ); double discriminant = b*b - 4.0*c; double d = Math.sqrt(discriminant); System.out.println( (-b + d) / 2.0); System.out.println( (-b - d) / 2.0); $ java Quadratic -1.0-1.0 1.618033988749895-0.6180339887498949 $ java Quadratic 1.0 1.0 NaN NaN 11
משתנים בוליאניים משתנה יכול לקבל רק אחד משני ערכים:,! פעולות לוגיות &&,, האופרטור && AND האופרטור OR! האופרטור NOT a b a && b a b a b a!a if ( test_grade >= 60 && exercise_grade>=60 ) System.out.println ( You passes the course ); דוגמא 12
אופרטורים להשוואה משמעות y קטן מ x y קטן או שווה ל x y גדול מ x y גדול או שווה x y שווה ל x y שונה מ x אופרטור > >= > >= ==!= דוגמא x < y x <= y x > y x >= y x == y x!= y 13
תוכנית להשוואת מספרים public class IsOrdered { תוכנית הבודקת האם שלושה מספרים מסודרים בסדר עולה public static void main(string[] args) { double a = Double.parseDouble( args[0] ); double b = Double.parseDouble( args[1] ); double c = Double.parseDouble( args[2] ); boolean firsttwo = (a <= b); boolean lasttwo = (b <= c); boolean allthree = firsttwo && lasttwo; System.out.println("Are the numbers ordered? " + allthree); $ java IsOrdered 1 2 2.5 Are the numbers ordered? $ java IsOrdered 1 6 2.5 Are the numbers ordered? 14
שיטות מתמטיות עיקריות double Math.E() Value of e, 2.718282 double Math.PI() Value of pi, 3.14159265 double Math.sin(ar) Returns the sine of ar. double Math.cos(ar) Returns the cosine of ar. double Math.tan(ar) Returns the tangent of ar. מספרים אקראיים קבועים מתמטיים שיטות טריגונומטריות double Math.sqrt(d) Returns the square root of d. t Math.abs(x) Returns absolute value of x with same type as the parameter: int, long, float, or double. t Math.max(x, y) Returns maximum of x and y with same type as the parameter: int, long, float, or double. t Math.min(x, y) Returns minimum of x and y with same type as the parameter: int, long, float, or double. double Math.random() Returns a number x in the range, 0.0 <= x < 1.0. מספרים אקראיים 15
מעבר בין סוגי משתנים double מעבר בטוח אין איבוד מידע int double בטוח לא מעבר יש איבוד מידע int 16
Type conversion - error public class TypeConversion { public static void main(string[] args) { double a = 3.5; int n = a; System.out.println(n); $ javac TypeConversion.java TypeConversion.java:5: possible loss of precision found : double required: int int n = a; ^ 1 error $ 17
Type conversion public class TypeConversion1 { public static void main(string[] args) { double a = 3.5; int n = (int) a; System.out.println(n); $ javac TypeConversion1.java dror@dror-1 ~/java $ java TypeConversion1 3.4 3 18
סיכום מעבר בין משתנים ערך 12345 123 3 4 4 3.3 3.3 0 3 סוג String int int long int double double int int ביטוי 1234 + 56 Integer.parseInt( 123 ); (int) 3.345 Math.Round(3.6758) (int) Math.Round(3.6758) 11 * 0.3 (int) 11 * 0.3 11 * (int) 0.3 (int) (11 * 0.3) 19